
namespace eval ::SpellSystem {
	variable VERSION 0.81
	variable ENABLE 1
	variable BUFF_TYPE 2
	variable USE_CONF 1
	if {$USE_CONF} {
	::Custom::LoadConf
	}
}

proc ::SpellSystem::QueueSystem { to from spell_list } {
	foreach x $spell_list { 
	if { ![info exist ::spellqueue($to)] } { 
	set ::spellqueue($to) "$x" 
	} else {
	set add 0
	foreach z $::spellqueue($to) {
	if { $z == $x } { set add 1 }
	}
	if { $add == 0 } {
	append ::spellqueue($to) " $x"
	}
	}
	}
	return
}

proc ::SpellSystem::Command { player cargs } {
	::SpellSystem::CommandSender $player
	return
}

proc ::SpellSystem::CommandSender { player } {
	::SpellSystem::AIQueue $player
	return
}

proc ::SpellSystem::AIQueue { victim } {
	if { [info exist ::spellqueue($victim)] } {
	set time [clock seconds]
	if { ![info exist ::lastcast($victim)] } {
	#puts "$::lastcast($victim)"
	set spells [split $::spellqueue($victim) " "]
	set spell [lindex $spells 1]
	set spells ""
	foreach x $::spellqueue($victim) {
	if { $x != $spell } { append spells " $x" }
	}
	set ::spellqueue($victim) $spells
	set ::lastcast($victim) $time
	CastSpell $victim $victim $spell
	} elseif { [info exist ::lastcast($victim)] } {
	if { $time > [expr {$::lastcast($victim) + 2}] } {
	set spells [split $::spellqueue($victim) " "]
	set spell [lindex $spells 1]
	set spells ""
	foreach x $::spellqueue($victim) {
	if { $x != $spell } { append spells " $x" }
	}
	if { $spell != "" } {
	set ::spellqueue($victim) $spells
	set ::lastcast($victim) $time
	CastSpell $victim $victim $spell
	}
	}
	}
	}
}

proc ::SpellSystem::Stats { type num } {
	if { $num > 0 && $num < 47 } {
	switch $type {
	"agility" { 
	set startspell 40999
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"strength" {
	set startspell 41045
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"spirit"	{
	set startspell 41091
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"crit"	{
	set startspell 41137
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"block"	{
	set startspell 41183
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"pary"	{
	set startspell 41229
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"doge"	{
	set startspell 41275
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"speed"	{
	set startspell 41321
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"spellcrit" {
	set startspell 41367
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicholy" {
	set startspell 41413
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicfire" {
	set startspell 41459
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicnature" {
	set startspell 41505
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicfrost" {
	set startspell 41551
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicshadow" {
	set startspell 41597
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicarcane" {
	set startspell 41643
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"map" {
	set startspell 41689
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"rap" {
	set startspell 41735
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"mh" { set spell [::SpellSystem::Stats magicholy $num];return $spell }
	"mn" { set spell [::SpellSystem::Stats magicnature $num];return $spell }
	"mfr" { set spell [::SpellSystem::Stats magicfrost $num];return $spell }
	"mfi" { set spell [::SpellSystem::Stats magicfire $num];return $spell }
	"ms" { set spell [::SpellSystem::Stats "magicshadow $num];return $spell }
	"ma" { set spell [::SpellSystem::Stats magicarcane $num];return $spell }
	"sc" { set spell [::SpellSystem::Stats spellcrit $num];return $spell }
	"spd" { set spell [::SpellSystem::Stats speed $num];return $spell }
	"agi" { set spell [::SpellSystem::Stats agility $num];return $spell  }
	"str" { set spell [::SpellSystem::Stats strength $num];return $spell  }
	"spi" { set spell [::SpellSystem::Stats spirit $num];return $spell  }
	defauld { return 0 }
	}
	}
	if { $num > -47 && $num < 0 } {
	switch $type {
	"agility" { 
	set startspell 41781
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"strength" {
	set startspell 41827
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"spirit"	{
	set startspell 41873
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"crit"	{
	set startspell 41919
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"block"	{
	set startspell 41965
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"pary"	{
	set startspell 42011
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"doge"	{
	set startspell 42057
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"speed"	{
	set startspell 42103
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"spellcrit" {
	set startspell 42149
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicholy" {
	set startspell 42195
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicfire" {
	set startspell 42241
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicnature" {
	set startspell 42287
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicfrost" {
	set startspell 42333
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicshadow" {
	set startspell 42379
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"magicarcane" {
	set startspell 42425
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"map" {
	set startspell 42472
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"rap" {
	set startspell 42518
	set num [expr {$num*(-1)}]
	set spell [expr {$startspell+$num}]
	return $spell
	}
	"mh" { set spell [::SpellSystem::Stats magicholy $num];return $spell }
	"mn" { set spell [::SpellSystem::Stats magicnature $num];return $spell }
	"mfr" { set spell [::SpellSystem::Stats magicfrost $num];return $spell }
	"mfi" { set spell [::SpellSystem::Stats magicfire $num];return $spell }
	"ms" { set spell [::SpellSystem::Stats magicshadow $num];return $spell }
	"ma" { set spell [::SpellSystem::Stats magicarcane $num];return $spell }
	"sc" { set spell [::SpellSystem::Stats spellcrit $num];return $spell }
	"spd" { set spell [::SpellSystem::Stats speed $num];return $spell }
	"agi" { set spell [::SpellSystem::Stats agility $num];return $spell  }
	"str" { set spell [::SpellSystem::Stats strength $num];return $spell  }
	"spi" { set spell [::SpellSystem::Stats spirit $num];return $spell  }
	defauld { return 0 }
	}
	}
}

proc ::SpellSystem::QueueBuffs { to from spellid } {
	if { [info exist ::spellqueue($from)] } {
	#puts SpellQueue:$::spellqueue($from)
	set spells [split $::spellqueue($from) " "]
	#puts "Spells: $spells"
	set spell [lindex $spells 0]
	set spells ""
	#puts "Spell1: $spell"
	foreach x $::spellqueue($from) {
	if { $x != $spell && $x != $spellid } { append spells "$x " }
	}
	set i 0
	#puts "Spell: $spell $spellid"
	#puts "Spells: $spells"
	if {($spells == "" || $spells == " ") && ($spell=="" || $spell== " ")} {
	unset ::spellqueue($from)
	return
	}
	set ::spellqueue($from) $spells
	#if { $spells == "" } { return }
	CastSpell $from $from $spell
	} else { return }
	return
} 

proc ::SpellSystem::DoSpellScripts { } {
	for { set i 41000 } { $i <= 42563 } { incr i } {
	::Custom::AddSpellScript "::SpellSystem::QueueBuffs" $i
	}
	return
	}
	if { $::SpellSystem::ENABLE == 1 } {
	if { $::SpellSystem::BUFF_TYPE  == 2 } { ::SpellSystem::DoSpellScripts }
	if { $::SpellSystem::BUFF_TYPE  == 1 } { ::Custom::AddCommand "statupdate" ::SpellSystem::Command 0 }
	if { $::SpellSystem::BUFF_TYPE == 0 } { ::Custom::HookProc "AI::CanAgro" { ::SpellSystem::AIQueue $victim } }
}

::Custom::AddSpellScript "::SpellSystem::QueueBuffs" 696 7219 7516 7518 7569 7570 7576 7582 7597 7598 7681 7688 7708 7711 9141 9142 9305 9306 9314 9315 9316 9317 9318 9326 9327 9329 9331 9332 9335 9336 9342 9343 9344 9345 9346 9358 9396 9397 9398 9400 9402 9404 9406 9407 9408 9412 9414 9415 9416 9417 13383 13384 13385 13387 13390 13597 13601 13665 13669 13670 13675 13676 13881 14027 14047 14049 14052 14054 14055 14089 14097 14127 14248 14254 14798 14799 14803 14829 15438 15464 15465 15693 15715 15717 15806 15809 17371 17493 17819 17830 17867 17871 18013 18029 18030 18032 18033 18034 18035 18037 18049 18056 18185 18185 18378 18379 18384 18985 19691 20732 20885 20886 20959 21079 21142 21185 21346 21347 21348 21361 21362 21363 21364 21365 21366 21432 21433 21434 21442 21445 21518 21595 21596 21598 21599 21600 21601 21618 21619 21620 21623 21624 21625 21626 21627 21629 21636 21958 21969 21991 22188 22618 22620 22801 22811 22836 22849 22852 22855 22912 22988 23043 23046 23049 23101 23157 23172 23181 23210 23212 23217 23236 23264 23265 23515 23516